import org.w3c.dom.ls.LSException;

import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 123
 * Date: 2023-05-19
 * Time: 19:25
 */
public class MySingleList {

     static class ListNode{
       public int val;
       public ListNode next ;

       public ListNode(int data){
           this.val = data ;
       }

         @Override
         public String toString() {
             return  this.val+"  " ;
          }
     }
     public ListNode head ;
     public void createList( ){
         ListNode node1 = new ListNode(1) ;
         ListNode node2 = new ListNode(2) ;
         ListNode node3 = new ListNode(3) ;
         ListNode node4 = new ListNode(4) ;
         ListNode node5 = new ListNode(5) ;
         node1.next = node2 ;
         node2.next = node3 ;
         node3.next = node4 ;
         node4.next = node5 ;
         node5.next = null ;
        this. head = node1 ;

     }
     public void distPlay(){
       ListNode p = this.head ;
       if(p == null){
           System.out.println("链表中没有数据");
           return;
       }
       while(p != null){
           System.out.print(p +"  ");
           p = p.next ;
       }

     }

     public int size(){
         int listSize = 0 ;
         ListNode pos = this.head;
         while(pos != null){
             listSize ++ ;
             pos = pos .next ;
         }

         return listSize;
     }
     public boolean contains(int key ){
         ListNode pos = this.head ;
         while(pos != null){
           if(pos.val == key ) {
               return true;
           }
           pos = pos.next ;
         }
         return false ;
     }
 public void addFirst(int data){
         if(this.head == null){
             ListNode listNode = new ListNode(data) ;
             this.head = listNode ;
             return;
         }
         ListNode listNode = new ListNode(data) ;
         listNode.next = this.head ;
         this.head = listNode ;

 }

 public void addFast(int data){
         if(this.head == null){
             ListNode listNode = new ListNode(data) ;
             this.head = listNode ;
             return;
         }
     ListNode listNode = new ListNode(data) ;
     ListNode pos = this.head ;
     if(pos == null){
         head = listNode ;
         return;
     }else {
         while (pos.next != null) {
             pos = pos.next;
         }
         pos.next = listNode;
         listNode.next = null;
     }
 }
 public void addIndex(int pos,int data){
        if(pos<0 || pos > size()){
            System.out.println(pos+" 位置不合法");
            return;
        }else if(pos == 0){
            addFirst(data);
            return;
        }else if(pos == size()){
            addFast(data);
            return ;
        }
//        ListNode p = this.head ;
//        for(int i = 1 ; i < pos - 1 ;i ++){
//            p = p.next ;
//        }
          ListNode p = findIndexNode(pos) ;
        ListNode listNode = new ListNode(data) ;
        listNode.next = p.next ;
        p.next = listNode ;
 }
   private ListNode findIndexNode(int index) {
       ListNode listNode = this.head;
       while(index -1 != 0){
           listNode = listNode.next ;
           index-- ;
       }
          return listNode ;
   }

    public void remove(int key){
        if(size() == 0){
            System.out.println("链表中没有数据");
            return;
        }
        if(head .val == key){
            head = head.next ;
            return;
        }
       ListNode node = searchPrev(key) ;
        if(node == null){
            System.out.println("没有找到要删除的数据");
            return;
        }
        node .next = node.next.next ;
     }
     private ListNode searchPrev(int key){
         ListNode node = this.head ;
         while(node .next != null){
             if(node.next.val == key){
                 return node ;
             }else {
                 node = node.next ;
             }

         }
         return null ;
     }
    public void clear(){
         this.head = null;
    }

    public void deleteAllKey(int key){
         if(this.head == null){
             System.out.println("没有数据要删除");
             return;
         }
         ListNode pre = this.head ;
         ListNode cur = pre.next ;
         while(cur != null){
             if(cur.val == key){
                 pre .next = cur.next ;
                 cur = cur .next ;
             }else {
                 pre = cur ;
                 cur = cur.next ;
             }
         }
         if(head.val == key){
             head = head.next ;
         }



    }

}

